# -*- coding: utf-8 -*-
# #############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

#
# Please note that these reports are not multi-currency !!!
#

from osv import fields, osv
import tools


class multi_mrp_report(osv.osv):
    _name = "multi.mrp.report"
    _description = "Multi Mrp Report"
    _auto = False
    _columns = {
        'name': fields.char('Sequence', size=64, required=False, readonly=True),
        'quantity': fields.float('Quantity', readonly=True),
        'date': fields.date('Date', size=128, readonly=True),
        'day': fields.char('Day', size=128, readonly=True),
        'month': fields.selection([('01', 'January'), ('02', 'February'), ('03', 'March'), ('04', 'April'),
                                   ('05', 'May'), ('06', 'June'), ('07', 'July'), ('08', 'August'), ('09', 'September'),
                                   ('10', 'October'), ('11', 'November'), ('12', 'December')], 'Month', readonly=True),
        'year': fields.char('Year', size=4, readonly=True),
        'product_id': fields.many2one('product.product', 'Product', readonly=True),
        'category_id': fields.many2one('product.category', 'Category', readonly=True),
        'employee_id': fields.many2one('hr.employee', 'Employee', readonly=True),
    }

    def init(self, cr):
        tools.sql.drop_view_if_exists(cr, 'multi_mrp_report')
        cr.execute("""
            create or replace view multi_mrp_report as (
                SELECT imm.employee_id as employee_id, 
                        imm.date as date, 
                        imm.name as name, 
                        to_char(imm.date, 'YYYY') as year,
                        to_char(imm.date, 'MM') as month,
                        to_char(imm.date, 'YYYY-MM-DD') as day, 
                        immd.id as id,
                        immd.product_id as product_id, 
                        immd.quantity as quantity, 
                        immd.categ_id as category_id
                       
                    
                FROM init_multi_mrp_detail immd
                    INNER JOIN init_multi_mrp imm on (immd.detail_id = imm.id) 
                GROUP BY
                    imm.name,
                    imm.employee_id,
                    imm.date,
                    immd.product_id, 
                    immd.id, 
                    immd.categ_id
        
            )
        """)


multi_mrp_report()

